export default {
  props: {
    config: {
      type: Object,
      default: () => {
        return {}
      }
    },

    data: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },

  data() {
    return {
      activeName: '1',
      show: {},
      showData: {}
    }
  },
  computed: {},

  watch: {
    config: {
      handler(newVal, oldVal) {
        // console.log('watch,config, ', newVal, oldVal)
        const activeName =
          (newVal.propForView.notebook &&
            newVal.propForView.notebook.default) ||
          ''
        this.activeName = activeName
      },
      deep: true,
      immediate: true
    }
  },

  async created() {},

  methods: {
    fieldsDictToList(fields, metadatas) {
      const metadatas2 = metadatas || this.config.fields

      const fs = fields || {}
      const fs2 = Object.keys(fs).map(item => {
        const me = fs[item] || {}
        const meta = metadatas2[item]
        // console.log('fieldsDictToList,meta,', item, meta)
        const label = me.label !== undefined ? me.label : meta.string
        const placeholder =
          me.placeholder !== undefined ? me.placeholder : meta.string
        const rules = me.rules || [
          { required: true, message: `请填写 ${meta.string} ` }
        ]

        const ret = {
          name: item,
          ...meta,
          ...me,
          label,
          placeholder,
          rules,
          // fields: ,
          Model: this.config.Model.env('null')
        }

        if (['one2many', 'many2many'].includes(meta.type)) {
          const refModel = this.config.Model.env(meta.relation)
          ret.fields = refModel.metadata.fields
        }

        return ret
      })

      // console.log('fieldsDictToList', fs2)
      return fs2
    },

    getWorkflowState() {
      const propForView = this.config.propForView || {}
      const wkf = propForView.workflow || {}
      const state = wkf.state

      if (state) {
        return state(this.data)
      } else {
        return this.data.state
      }
    },

    getWorkflowSteps() {
      const propForView = this.config.propForView || {}
      const wkf = propForView.workflow || {}
      const steps = wkf.steps || {}
      // console.log('ViewMixin, getWorkflowSteps, ,', steps)
      return steps
    },

    getWorkflowButtons() {
      const propForView = this.config.propForView || {}
      const wkf = propForView.workflow || {}
      const buttons = wkf.buttons || {}
      // console.log('ViewMixin, getWorkflowButtons, ,', buttons)
      return buttons
    },

    getConfigFields() {
      const fs =
        this.config.propForView.fields ||
        Object.keys(this.config.fields).reduce((acc, cur) => {
          acc[cur] = null
          return acc
        }, {})

      // console.log('ViewMixin, getConfigFields, fs, ', fs, this.config)

      return this.fieldsDictToList(fs)
    },
    getPages() {
      if (!this.config) {
        return []
      } else if (!this.config.propForView) {
        return []
      } else if (!this.config.propForView.notebook) {
        return []
      } else if (!this.config.propForView.notebook.pages) {
        return []
      } else {
        return [...this.config.propForView.notebook.pages]
      }
    },

    checkOne2many(page) {
      // console.log('checkOne2many,page,', page)
      if (page.fields && Object.keys(page.fields).length === 1) {
        const fld_name = Object.keys(page.fields)[0]
        const me = page.fields[fld_name] || {}
        const meta = this.config.fields[fld_name]
        const label = me.label !== undefined ? me.label : meta.string

        if (['one2many', 'many2many'].includes(meta.type)) {
          const Model = this.config.Model

          const refModel = Model.env(meta.relation)
          // console.log('checkOne2many, refModel,', refModel)

          page._one2many = {
            name: fld_name,
            ...me,
            ...meta,
            label,
            fields: refModel.metadata.fields,
            Model
          }

          // console.log('checkOne2many, page._one2many,', page._one2many)
          // console.log('checkOne2many, this.config,', this.config)

          return true
        }
      }
      return false
    }
  }
}
